package oo

import (
	"elegantGo/chapter17/controller/response"
	"elegantGo/chapter17/oo/primitive"
)

type ArticleOrders []ArticleOrder

type ArticleOrder interface {
	GetID() int
	SetID(id int)
	GetUserID() int
	SetUserID(v int)
	GetArticleID() int
	SetArticleID(v int)
	GetPrice() int
	SetPrice(v int)

	Mapper() *response.ArticleOrder
}

func NewArticleOrder() ArticleOrder {
	return new(BaseArticleOrder)
}

func (oo ArticleOrders) IsPaid(sessionUser User) bool {
	for _, order := range oo {
		if order.GetUserID() == sessionUser.GetID() {
			return true
		}
	}

	return false
}

func (oo ArticleOrders) Mapper() response.ArticleOrders {
	size := len(oo)
	respArticleOrders := make(response.ArticleOrders, size)
	for i := 0; i < size; i++ {
		respArticleOrders[i] = oo[i].Mapper()
	}
	return respArticleOrders
}

type BaseArticleOrder struct {
	id        primitive.ID
	userID    primitive.ID
	articleID primitive.ID
	price     primitive.Fen
}

func (oo *BaseArticleOrder) GetID() int {
	return oo.id.Value()
}

func (oo *BaseArticleOrder) SetID(v int) {
	oo.id.SetValue(v)
}

func (oo *BaseArticleOrder) GetUserID() int {
	return oo.userID.Value()
}

func (oo *BaseArticleOrder) SetUserID(v int) {
	oo.userID.SetValue(v)
}

func (oo *BaseArticleOrder) GetArticleID() int {
	return oo.articleID.Value()
}

func (oo *BaseArticleOrder) SetArticleID(v int) {
	oo.articleID.SetValue(v)
}

func (oo *BaseArticleOrder) GetPrice() int {
	return oo.price.Value()
}

func (oo *BaseArticleOrder) GetPriceYuan() float64 {
	return oo.price.ToYuan()
}

func (oo *BaseArticleOrder) SetPrice(v int) {
	oo.price.SetValue(v)
}

func (oo *BaseArticleOrder) Mapper() *response.ArticleOrder {
	if oo == nil {
		return nil
	}
	respArticleOrder := new(response.ArticleOrder)
	respArticleOrder.ID = oo.GetID()
	respArticleOrder.UserID = oo.GetUserID()
	respArticleOrder.Price = oo.GetPriceYuan()
	return respArticleOrder
}
